package com.jindy.module.dict.service.impl;

import com.jindy.module.dict.domain.Region;
import com.jindy.module.dict.mapper.RegionMapper;
import com.jindy.module.dict.service.RegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 地区Service业务层处理
 *
 * @author minjunjie
 * @date 2021-06-28
 */
@Service
public class RegionServiceImpl implements RegionService
{
    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询地区
     *
     * @param id 地区ID
     * @return 地区
     */
    @Override
    public Region selectRegionById(String id)
    {
        return regionMapper.selectRegionById(id);
    }

    /**
     * 查询地区列表
     *
     * @param region 地区
     * @return 地区
     */
    @Override
    public List<Region> selectRegionList(Region region)
    {
        return regionMapper.selectRegionList(region);
    }

    /**
     * 获取城市列表
     *
     * @param provinceName
     * @return
     */
    @Override
    public List<Region> selectCityNameList(String provinceName) {
        return regionMapper.selectCityNameList(provinceName);
    }

    /**
     * 获取区/县列表
     *
     * @param cityName
     * @return
     */
    @Override
    public List<Region> selectDistrictNameList(String cityName) {
        return regionMapper.selectDistrictNameList(cityName);
    }

    @Override
    public List<Region> selectRegionProvinceList(Region region) {
        return regionMapper.selectRegionProvinceList(region);
    }

    @Override
    public List<Map<String, Object>> selectTreeList() {
        //省集合
        List<Region> ps=regionMapper.selectProvinceList();

        //省集合
        List<Region> cs=regionMapper.selectCityList();
        //省集合
        List<Region> ds=regionMapper.selectDistrictList();

        //封装省市区
        List<Map<String, Object>> arrayProvinceList = new ArrayList<Map<String, Object>>();
        //遍历
        for (Region provinces : ps) {
            ArrayList arrayCityList = new ArrayList();
            Map<String, Object> provinceMap = new HashMap<>();
            //遍历
            for (Region citys : cs) {
                if(citys.getCityCode().startsWith(provinces.getCode())){
                    ArrayList arrayDistrictList = new ArrayList();
                    Map<String, Object> cityMap = new HashMap<>();
                    //获取区/县列表
                    for (Region districts : ds) {
                        if(districts.getDistrictCode().startsWith(citys.getCityCode())){
                            Map<String, String> districtMap = new HashMap<>();
                            districtMap.put("label", districts.getDistrictName());
                            districtMap.put("value", districts.getDistrictCode());
                            arrayDistrictList.add(districtMap);
                        }
                    }
                    cityMap.put("label", citys.getCityName());
                    cityMap.put("value", citys.getCityCode());
                    cityMap.put("children", arrayDistrictList);
                    arrayCityList.add(cityMap);
                }
            }
            provinceMap.put("label", provinces.getProvinceName());
            provinceMap.put("value", provinces.getCode());
            provinceMap.put("children", arrayCityList);
            arrayProvinceList.add(provinceMap);
        }
        return arrayProvinceList;
    }
}
